#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 6/4/2018 9:53 AM
# @Author  : Edwin
# @Version : Python 3.6
# @File    : test.py

import pandas as pd
import time

print(time.localtime(time.time()))
event_data = pd.read_csv("events.csv")
uniqueEvents = pd.read_csv("uniqueEvents.csv")

import threading,time
import math

df1 = uniqueEvents.iloc[:,0]
df_list = sorted(list(df1))


def mid_search(start,end,val):

    while start <=end:

        mid = (start+end)//2
        if (df_list[mid] == int(val)):

            return True
        elif df_list[mid] > val:
            end = mid - 1
        else:
            start = mid + 1
    return False

def event_process(i,l):
    try:
        event_data_ = event_data[i:i+1]
        if mid_search(0,len(df_list)-1,event_data_["event_id"].values[0]):
            l[i] = event_data_
    except Exception as x:
        pass

def child_thread(start,end,result):

    threadpool = []

    for i in range(start, end, 1):
        th = threading.Thread(target=event_process, args=(i, result))
        threadpool.append(th)

    for th in threadpool:
        th.start()

    for th in threadpool:
        threading.Thread.join(th)


if __name__ == '__main__':

    result = dict()

    total_len = len(event_data)
    n = math.ceil(total_len/4000)

    for part in range(n):
        child_thread(part*4000,part*4000+4000,result)
        print("time is {}, finished part is {}".format(time.localtime(time.time()),part))


    event_pd = pd.DataFrame(columns=event_data.columns)
    for key,item in result.items():
        event_pd = event_pd.append(item)

    print('result is {}, total numner is {} '.format(event_pd.head(10),len(event_pd)))
    print(time.localtime(time.time()))

    event_pd.to_csv("final_event.csv")
    # to_csv()    方法的encoding参数默认为    "gbk"，而read_csv()    方法的encoding参数默认为
    # "utf-8"，所以最好的防止由于编码出错导致读取错误的方法就是在to_csv()和read_csv() 方法同时显式地设置相同的encoding参数。